package com.wust.menu;

import java.util.Iterator;
import java.util.List;

import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

import com.wust.R;
import com.wust.baby.Baby;
import com.wust.baby.BabyDao;
import com.wust.baby.BabyInfo;
import com.wust.baby.BabyInfoDao;
import com.wust.data.Data;
import com.wust.data.DataDao;
import com.wust.database.KnowledgeDatabase;
import com.wust.footprint.LoginActivity;
import com.wust.mode.Mode;
import com.wust.mode.ModeDao;
import com.wust.user.User;
import com.wust.user.UserDao;

public class MainActivity extends TabActivity {
    /** Called when the activity is first created. */
	public TabHost mth;
	public static final String TAB_FOOT="成长足迹";
	public static final String TAB_TIESHI="生活小贴士";
	public static final String TAB_ANGEL="爱心天使";
	public static final String TAB_WEIBO="爱心社区";
	public static final String TAB_AD="精品推荐";
	public RadioGroup radioGroup;
	private UserDao userDao;
	private User user;
	private String user_id;
	private String info;
	private  BabyDao babyDao;
	private Baby baby;
	private  BabyInfo babyInfo;
	private KnowledgeDatabase database;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
      
        
        babyDao=new BabyDao(this);
        BabyInfoDao infoDao=new BabyInfoDao(this);
        userDao=new UserDao(this);
        ModeDao modeDao=new ModeDao(this);
        Mode mode=modeDao.getModeInfo("男", "0");
        if(mode.getMode_height()==0)
        	modeDao.insertMode();
		user=userDao.checkUserHasLogin();
		 database=new KnowledgeDatabase(this);
		if(user.getUser_id()<1)
			{
			System.out.println(" user : user");
			  Intent intent=new Intent(this,LoginActivity.class);
			  startActivity(intent);
			 // finish();
			}
		else if(user.getUser_pass().equals("")||user.getUser_pass()==null)
		{
			
			 baby=babyDao.getBabyByUserName(user.getUser_name());
			user.setUser_haslogin(0);
			userDao.updateUser(user);
			user_id=String.valueOf(user.getUser_id());
			String time=baby.getBaby_date();
			if(time==null)
				time="2008-3-20";
			babyInfo=infoDao.getBabyInfoById(String.valueOf(baby.getBaby_id()),time);
			
			info="身高为:"+babyInfo.getInfo_height()+"cm "+"体重为:"+babyInfo.getInfo_weigth()+"kg";
	    	SharedPreferences preferences = getSharedPreferences("userinfo",Context.MODE_PRIVATE);
	    	
	    	Editor usereditor = preferences.edit();//获取编辑器  
	    	usereditor.putString("user_name", user.getUser_name()); 
	    	usereditor.putString("user_nickname", user.getUser_nickname());
	    	usereditor.putString("baby_id", String.valueOf(baby.getBaby_id()));
	    	usereditor.putString("baby_date", time);
	    	usereditor.putString("user_icon", user.getUser_icon());
	    	usereditor.putString("info", info);
	    	usereditor.commit();
		}
		else
		{
			 baby=babyDao.getBabyByUserName(user.getUser_name());
			 String time=baby.getBaby_date();
				if(time==null)
					time="2008-3-20";
				babyInfo=infoDao.getBabyInfoById(String.valueOf(baby.getBaby_id()),time);
				
				info="身高为:"+babyInfo.getInfo_height()+"cm 体重为:"+babyInfo.getInfo_weigth()+"kg";
				user_id=String.valueOf(user.getUser_id());
				SharedPreferences preferences = getSharedPreferences("userinfo",Context.MODE_PRIVATE);
		    	
		    	Editor usereditor = preferences.edit();//获取编辑器  
		    	usereditor.putString("user_name", user.getUser_name());  
		    	usereditor.putString("user_nickname", user.getUser_nickname());
		    	usereditor.putString("baby_id", String.valueOf(baby.getBaby_id()));
		    	usereditor.putString("user_icon", user.getUser_icon());
		    	usereditor.putString("baby_date", time);
		    	usereditor.putString("info", info);
		    	usereditor.commit();
		    	
		}
		
        mth=this.getTabHost();
        
        TabSpec ts1=mth.newTabSpec(TAB_FOOT).setIndicator(TAB_FOOT);
        ts1.setContent(new Intent(MainActivity.this,FirstGroupTab.class));
        mth.addTab(ts1);
        
        TabSpec ts2=mth.newTabSpec(TAB_TIESHI).setIndicator(TAB_TIESHI);
        ts2.setContent(new Intent(MainActivity.this,SecondGroupTab.class));
        mth.addTab(ts2);
        TabSpec ts3=mth.newTabSpec(TAB_ANGEL).setIndicator(TAB_ANGEL);
        ts3.setContent(new Intent(MainActivity.this,ThirdGroupTab.class));
        mth.addTab(ts3);
        TabSpec ts4=mth.newTabSpec(TAB_WEIBO).setIndicator(TAB_WEIBO);
        ts4.setContent(new Intent(MainActivity.this,FourthGroupTab.class));
        mth.addTab(ts4);
        TabSpec ts5=mth.newTabSpec(TAB_AD).setIndicator(TAB_AD);
        ts5.setContent(new Intent(MainActivity.this,FifthGroupTab.class));
        mth.addTab(ts5);
        this.radioGroup=(RadioGroup)findViewById(R.id.main_radio);
        radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				
				switch(checkedId){
				case R.id.radio_button0:
					mth.setCurrentTabByTag(TAB_FOOT);
					break;
				case R.id.radio_button1:
					mth.setCurrentTabByTag(TAB_TIESHI);
					break;
				case R.id.radio_button2:
					mth.setCurrentTabByTag(TAB_ANGEL);
					break;
				case R.id.radio_button3:
					mth.setCurrentTabByTag(TAB_WEIBO);
					break;
				case R.id.radio_button4:
					mth.setCurrentTabByTag(TAB_AD);
					break;
				}
			}
		});
        
    }
    
}